using System;
using DG.Tweening;
using TMPro;
using UnityEngine;

namespace Bubbles
{
    public class Message : MonoBehaviour
    {
        public Direction Direction => direction;
        
        [SerializeField] private Direction direction;
        [SerializeField] private Bubble bubble;

        [SerializeField] private SpriteRenderer portrait;
        [SerializeField] private TypingIndicator typingIndicator;

        [SerializeField] private float portraitShowTime = 0.3f;
        [SerializeField] private float bubbleShowTime = 0.3f;

        public bool IsTyping { get; private set; } = true;


        private void Start()
        {
            typingIndicator.SetVisibility(false);
        }

        private void OnEnable()
        {
            bubble.enabled = true;
        }

        private void OnDisable()
        {
            bubble.enabled = false;
        }

        public void ShowTyping()
        {
            Color transparent = portrait.color;
            transparent.a = 0f;
            portrait.color = transparent;
            portrait.DOFade(1f, portraitShowTime);
            
            typingIndicator.Show();
            bubble.gameObject.SetActive(false);
        }

        public void ShowMessage(string text)
        {
            IsTyping = false;
            bubble.SetText(text);
            typingIndicator.Hide();
            ShowBubble();
        }

        private void ShowBubble()
        {
            bubble.transform.localScale = Vector3.zero;
            bubble.transform.DOScale(1f, bubbleShowTime)
                .SetEase(Ease.OutExpo);
            bubble.gameObject.SetActive(true);
        }
    }
}
